package com.ocom.aiotweb.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.aiotweb.feignclient.AiotClientService;
import com.ocom.aiotweb.vo.ota.OtaListVo;
import com.ocom.common.constant.AiotUrl;
import com.ocom.common.request.aiot.OtaPushDetailVo;
import com.ocom.common.request.aiot.OtaPushVo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.utils.poi.ExcelUtil;
import com.ocom.common.vo.aiotweb.ImportOtaVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static cn.hutool.poi.excel.cell.CellUtil.getCellValue;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2022/8/18
 **/
@Slf4j
@RestController
public class OtaController {

	@Autowired
	AiotClientService aiotClientService;

	@PostMapping(AiotUrl.URLP_AIOTWEB_otaPush)
	public Result otaPush(@RequestBody OtaPushVo otaPushVo){
		log.info("Ota升级记录"+otaPushVo);
		return aiotClientService.otaPush(otaPushVo);
	}


	@PostMapping(AiotUrl.URLP_AIOTWEB_getOtaList)
	public Result getOtaList(@RequestBody OtaListVo listVo){
		log.info("列表记录"+listVo);
		return aiotClientService.getList(listVo.getPage(),listVo.getSize(),listVo.getDeviceNo());
	}


	/**
	 * 导入excel下载（getOtaExcel）
	 * */
	@GetMapping(AiotUrl.URLP_AIOTWEB_getOtaExcel)
	public void excel(HttpServletResponse response) throws IOException {
		ExcelUtil<ImportOtaVo> util = new ExcelUtil<>(ImportOtaVo.class);
		util.importTemplateExcel(response, "Ota信息");
	}

	@PostMapping(AiotUrl.URLP_AIOTWEB_importOtaPush)
	public Result importOta(@RequestPart("file") MultipartFile file){
		//获得文件名
		Workbook workbook = null ;
		String fileName = file.getOriginalFilename();
		if(fileName.endsWith("xls")){
			try {
				workbook = new HSSFWorkbook(file.getInputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else if(fileName.endsWith("xlsx")){
			try {
				workbook = new XSSFWorkbook(file.getInputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			try {
				throw new Exception("文件不是Excel文件");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		Sheet sheet = workbook.getSheetAt(0);
		int rows = sheet.getLastRowNum();// 指的行数，一共有多少行+
		if(rows==0){
			try {
				throw new Exception("请填写数据");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}


		OtaPushVo otaPushVo =new OtaPushVo();


		for (int i = 1; i < rows+1; i++) {


			// 读取左端单元格
			Row row = sheet.getRow(i);
			// 行不为空
			if (row != null) {

				OtaPushDetailVo otaPushDetailVo =new OtaPushDetailVo();

				// **读取cell**
				//姓名
				String pName = JSON.toJSONString(getCellValue(row.getCell(0))).replace("\"", "").replace("\\t", "");
				pName=pName.replace("\\t","");
				if (StringUtils.isNotEmpty(pName) && !pName.equals("null")){
					otaPushDetailVo.setDeviceNo(pName);
				}else{
					continue;
				}

				// 性别(男/女)
				String pSex = JSON.toJSONString(getCellValue(row.getCell(1))).replace("\"", "").replace("\\t", "");
				pSex=pSex.replace("\\t","");
				if (StringUtils.isNotEmpty(pSex) && !pSex.equals("null")){
					otaPushDetailVo.setAppVer(pSex);
				}else{
					continue;
				}
				otaPushVo.getList().add(otaPushDetailVo);

			}
		}
		log.info("Ota excel 升级记录"+otaPushVo);
		aiotClientService.otaPush(otaPushVo);

		return ResultUtil.success();
	}


	@PostMapping(AiotUrl.Url_webAiot_saveOtaFile)
	public Result saveOtaFile(@RequestBody JSONObject jsonObject){
		return aiotClientService.saveOtaFile(jsonObject.getString("appVer"),jsonObject.getString("url"),jsonObject.getInteger("type"));
	}

	@PostMapping(AiotUrl.Url_webAiot_otaFileList)
	public Result otaFileList(@RequestBody JSONObject jsonObject){
		return aiotClientService.otaFileList(jsonObject.getInteger("page"),jsonObject.getInteger("size"));
	}

}
